interface IWinMove {
  handleMouseDown: (event: MouseEvent) => void
}

export default function useWinMove(): IWinMove {
  let initialX: number
  let initialY: number

  const mousemoveFn = (event: MouseEvent): void => {
    // 鼠标屏幕上的坐标
    const screenX = event.screenX
    const screenY = event.screenY

    window.api.winMove({ x: screenX - initialX, y: screenY - initialY })
  }

  const handleMouseDown = (event: MouseEvent): void => {
    initialX = event.clientX
    initialY = event.clientY

    document.addEventListener('mousemove', mousemoveFn)

    document.addEventListener(
      'mouseup',
      () => {
        document.removeEventListener('mousemove', mousemoveFn)
      },
      { once: true }
    )
  }

  return {
    handleMouseDown
  }
}
